ChatBox.prototype.unalteredPostChat = ChatBox.prototype.postChat;
ChatPane.prototype.unalteredShow = ChatPane.prototype.show;
ChatPane.prototype.unalteredHide = ChatPane.prototype.hide;
ChatPane.prototype.unalteredAddLine = ChatPane.prototype.addLine;
var gChatUserLog;

$(document).ready(function(){
	customFilters.unshift(getUsername());
	for(var i in customFilters){
		customFilters[i] = customFilters[i].toLowerCase();
	}
});
function getUsername(){
	var regex = /Logged in as (.{0,15}) \|/g;
	var accountString = $("#account-status").text();
	var match = regex.exec(accountString);
	return match[1].toLowerCase();
}

function checkForFilters(text){
	for(var i in customFilters){
		if( text.indexOf(":") != -1 && text.indexOf(customFilters[i]) > text.indexOf(":") ) 
			return true;
	}
	return false;
}

ChatBox.prototype.postChat = function(a) {
    this.unalteredPostChat(a);
    var chat = a.text.toLowerCase();
    //checkForFilters(chat) && $("#UnreadChatNotification"+a.filter).css({"background":"red"});
};
ChatPane.prototype.show = function(){
	//this.mUnreadCountBubble.css({"background":""});
	this.unalteredShow();
};
ChatPane.prototype.hide = function(){
	//this.mUnreadCountBubble.css({"background":""});
	this.unalteredHide();
};
ChatPane.prototype.addLine = function(a, b, c){
	var chat = a[0].innerHTML.toLowerCase();
    //checkForFilters(chat) && a.css("background", "#FFF0F0");
	//emoticons && a.contents().filter(function() {  return this.nodeType == 3;}).wrap('<span class="chatLineText"/>');
	this.unalteredAddLine(a, b, c);
	//emoticons && a.children(".chatLineText").emoticonize({"delay":500, "animate":false, "exclude":"a"});
};

//Chat User Log to move login and logout messages from a channel to a scrolling semi-ticker
//
//

//Initialized in hook into character load

window.characterDataDownloaded = (function() {
	var unaltered = window.characterDataDownloaded;
	return function(a){
		//extending characterDataLoaded(data), extension goes last
		//initialize global ChatUserLog if it doesn't already exist
		unaltered(a);
		(gChatUserLog == null) && (gChatUserLog = new ChatUserLog);
	}
})();

function ChatUserLog() {
	ChatUserLog.mChatUserLog = this;
	this.mUserAlerts = [];
	this.mAlertBar = "#landcapeAlertBar";
	this.mBarHorizontalMarker = 6;
}

ChatUserLog.prototype.createUserAlert = function(data) {
	var newUserAlert = $('<div></div>');
	newUserAlert.addClass('landscape-alert icon-left');
	
	var newUserAlertContent = $('<span></span>');
	
	var friendOnlineStateChange = 'login';
	if (data.message == MESSAGE_TYPE.friendOffline) {
		friendOnlineStateChange = 'logout';
	}	
	
	var playerHandle = "<span class='playerHandle_"+data.characterID+"'>...</span>";
	
	var player = Player.playerWithId(data.characterID);
	if (player == null) {
		player = new Player(data.characterID);
		player.downloadDataWithCallback(null);
	} else {
		playerHandle = player.getHandle();
	}
	
	newUserAlert.addClass("icon-"+friendOnlineStateChange);
	
	newUserAlertContent.addClass('friendLink').bind(tapEvent, function() {
													showFriendProfile(data.characterID);
												}).html(playerHandle);
	
	newUserAlert.append(newUserAlertContent);
	
	this.addAlert(newUserAlert);
}

ChatUserLog.prototype.addAlert = function (userAlert) {
	$("#landscapeAlertBar").append(userAlert);
	this.mBarHorizontalMarker = 6;
	for(var i=0; i < this.mUserAlerts.length; i++){
		this.mBarHorizontalMarker += this.mUserAlerts[i].outerWidth();
	}
	userAlert.css("left", this.mBarHorizontalMarker+"px");
	this.mUserAlerts.push(userAlert);
	userAlert.animate({top:6}, 400).delay(50000).fadeOut(300, function(){ ChatUserLog.mChatUserLog.removeAlert(this); });
}

ChatUserLog.prototype.removeAlert = function (userAlert) {
	this.mUserAlerts.shift();
	var alertWidth = $(userAlert).outerWidth();
	$(".landscape-alert").animate({left:"-="+alertWidth}, {duration:200, queue:false});
	this.mBarHorizontalMarker-= alertWidth;
	if(this.mBarHorizontalMarker < 6)
		this.mBarHorizontalMarker = 6;
	$(userAlert).remove();
}

//Overwriting postFriend online with custom behavior
ChatBox.prototype.postFriendOnline = function(a){
	//intentionally left blank to maintain sanity, fill with online status scroller
	gChatUserLog.createUserAlert(a);
}

